package com.tcm.ucenter.web.controller;

import com.base.core.lang.StringUtils;
import com.base.core.utils.JacksonUtils;
import com.base.core.utils.R;
import com.base.logistics.sf.bean.print.PrintSfPushRequest;
import com.tcm.logistics.logic.sf.SfOrderLogicService;
import com.tcm.logistics.model.query.LogisticsQuery;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/logistics")
@RestController
public class ShunFengController {

    @DubboReference(version = "0.0.1", check = false, timeout = 3000)
    private SfOrderLogicService shunFengService;

    /**
     * 顺丰单号快递打印
     * @param
     * @return
     */
    @GetMapping("/shunfeng/print")
    public R shunFengPrint(@RequestParam String orderId, @RequestParam(defaultValue = "1") String searchType){
        try {
            return shunFengService.printData(orderId, searchType);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 顺丰单号快递查询
     * @param
     * @return
     */
    @GetMapping("/shunfeng/route")
    public R shunFengRoute(@RequestParam(defaultValue = "1") Integer trackingType,
                           @RequestParam String trackingNumber,
                           @RequestParam(defaultValue = "1") Integer methodType){
        Map<String, Object> res;
        try {
            if(StringUtils.isBlank(trackingNumber)) {
                return R.fail("请输入快递单号");
            }
            String[] trackingNumberArrays = trackingNumber.split(",");
            List<String> trackingNumbers = Arrays.asList(trackingNumberArrays);
            return shunFengService.queryRoute(trackingType, trackingNumbers,methodType);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 顺丰下单
     * @param
     * @return
     */
    @PostMapping("/shunfeng/order")
    public R createOrder(@RequestBody LogisticsQuery logisticsQuery){
        Map<String, Object> res = null;
        try {
            return shunFengService.createOrder(logisticsQuery.getContactInfos(),
                    logisticsQuery.getCargoDetails(),logisticsQuery.getOrderId(),
                    logisticsQuery.getRemark());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 顺丰下单
     * @param
     * @return
     */
    @PostMapping(value = "/shunfeng/push/print",produces = {"application/x-www-form-urlencoded;charset=UTF-8"})
    @ResponseBody
    public String pushPrint(@RequestParam Map<String, Object> query){
        Map<String, Object> ret = new HashMap<>();
        ret.put("success",false);
        try {

            PrintSfPushRequest request = new PrintSfPushRequest();
            BeanUtils.populate(request,query);
            R rs = shunFengService.printPushPdf(request);
            System.out.println("[推送]"+ JacksonUtils.toJSONString(rs));
            ret.put("msg","请求打印失败");
            if(rs.isSuccess()){
                ret.put("success",true);
                ret.put("msg","请求成功");
                return JacksonUtils.toJSONString(ret);
            }else {
                return JacksonUtils.toJSONString(ret);
            }
        }catch (Exception e){
            ret.put("msg", e.getMessage());
            return JacksonUtils.toJSONString(ret);
        }
    }

}
